package com.xn.app.muying.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.activity.DengLuActivity;
import com.xn.app.muying.activity.MainActivity;
import com.xn.app.muying.R;
import com.xn.app.muying.activity.HealthRecordsActivity;
import com.xn.app.muying.activity.JiuZhenActivity;
import com.xn.app.muying.activity.OrderListActivity;
import com.xn.app.muying.activity.PersonalActivity;
import com.xn.app.muying.activity.ScienceActivity;
import com.xn.app.muying.activity.SheetActivity;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.view.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 功能列表
 * Created by gaoge on 17/4/12.
 */
public class ContentFragment extends Fragment {
    //内容
    private String content = null;
    //处理数据的集合
    private ArrayList<HashMap<String, String>> functionGridList = new ArrayList<HashMap<String, String>>();
    //显示数据的gridview
    private MyGridView viewpage_gridview = null;
    //动作
    private Intent intent = new Intent();
    //数据库访问
    private DBManager db = null;
    //用户
    private String userName = null;

    public static ContentFragment newInstance(String content,
                                              ImageLoader imageLoader, DisplayImageOptions options) {
        // 对外提供创建实例的方法，你给我需要显示的内容，我给你Fragment实例
        ContentFragment instance = new ContentFragment();
        instance.content = content;
        return instance;
    }

    public ContentFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 初始化控件
        View view = inflater.inflate(R.layout.home_viewpager, container, false);
        viewpage_gridview = (MyGridView) view.findViewById(R.id.viewpage_gridview);
        db = new DBManager(getActivity());
        if (content.equals("0")) {
            functionGridList = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> functionMap = new HashMap<>();
            functionMap.put("image", R.mipmap.order + "");
            functionMap.put("name", "预约挂号");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", R.mipmap.huayandan + "");
            functionMap.put("name", "报告识别");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", R.mipmap.yishengjieshao + "");
            functionMap.put("name", "专家医师");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", R.mipmap.jiankangdangan + "");
            functionMap.put("name", "健康档案");
            functionGridList.add(functionMap);
        } else if (content.equals("1")) {
            functionGridList = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> functionMap = new HashMap<>();
            functionMap.put("image", R.mipmap.jianchakepu + "");
            functionMap.put("name", "检查科普");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", "");
            functionMap.put("name", "");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", "");
            functionMap.put("name", "");
            functionGridList.add(functionMap);
            functionMap = new HashMap<>();
            functionMap.put("image", "");
            functionMap.put("name", "");
            functionGridList.add(functionMap);
        }
        //加载适配器
        viewpage_gridview.setAdapter(new ViewAdapter(getActivity(), functionGridList));
        //点击事件
        viewpage_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                userName = db.select("tel");
                if (content.equals("0")) {
                    //第一页
                    switch (position) {
                        //预约挂号
                        case 0:
                            if (userName != null && !userName.equals("") && !userName.equals("null")) {
                                intent.setClass(getActivity(), OrderListActivity.class);
                                startActivity(intent);
                            } else {
                                //如果没有登陆，调整到登陆页面
                                startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                            }
                            break;
                        //报告识别
                        case 1:
                            if (userName != null && !userName.equals("") && !userName.equals("null")) {
                                intent.setClass(getActivity(), SheetActivity.class);
                                startActivity(intent);
                            } else {
                                //如果没有登陆，调整到登陆页面
                                startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                            }
                            break;
                        //专家医师
                        case 2:
                            intent.putExtra("SIGN", "introduction");
                            intent.setClass(MainActivity.mActivity, JiuZhenActivity.class);
                            startActivity(intent);
                            break;
                        //健康档案
                        case 3:
                            //检查是否登陆
                            if (userName != null && !userName.equals("") && !userName.equals("null")) {
                                //判断用户是否填写过身份信息
                                if (db.select("identityId") != null && !db.select("identityId").equals("") && !db.select("identityId").equals("null")) {
                                    startActivity(new Intent(MainActivity.mActivity, HealthRecordsActivity.class));
                                } else {
                                    startActivity(new Intent(MainActivity.mActivity, PersonalActivity.class).putExtra("type", "1"));
                                }
                            } else {
                                //如果没有登陆，调整到登陆页面
                                startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                            }
                            break;

                    }
                } else if (content.equals("1")) {
                    //第二页
                    switch (position) {
                        //检查科普
                        case 0:
                            intent.setClass(getActivity(), ScienceActivity.class);
                            startActivity(intent);
                            break;
                    }
                }
            }
        });
        return view;
    }

    //适配器
    public class ViewAdapter extends BaseAdapter {
        LayoutInflater lf = null;
        ArrayList<HashMap<String, String>> getData = new ArrayList<HashMap<String, String>>();
        ViewHolder viewHolder = null;

        public ViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> getData) {
            lf = LayoutInflater.from(context);
            this.getData = getData;
        }

        @Override
        public int getCount() {
            return getData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = lf.inflate(R.layout.viewpager_grid_item, null);
                viewHolder.textview = (TextView) convertView.findViewById(R.id.textview);
                viewHolder.imageview = (ImageView) convertView.findViewById(R.id.imageview);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textview.setText(getData.get(position).get("name"));
            if (getData.get(position).get("image") != null && !getData.get(position).get("image").equals("")) {
                viewHolder.imageview.setImageResource(Integer.parseInt(getData.get(position).get("image")));
            }
            return convertView;
        }
    }

    /**
     * 包装类
     */
    class ViewHolder {
        public TextView textview = null;
        public ImageView imageview = null;
    }
}
